package file.config;

import file.util.FileUtil;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author hh
 * @since 2025/3/6
 */
public class FsConfig {

    public static final String CONFIG_FILE_NAME = "fs.config";

    private static final List<String> configs = new CopyOnWriteArrayList<>();

    public static String get(String configName) throws Exception {
        if (!StringUtils.hasText(configName)) {
            throw new Exception("配置名称为空!");
        }
        if (configs.isEmpty()) {
            loadConfig();
        }
        for (String config : configs) {
            String configContent = getConfigContent(config, configName);
            if (configContent != null) {
                return configContent;
            }
        }
        return null;
    }

    public static void save(String configName, String content) throws Exception {
        if (configs.isEmpty()) {
            loadConfig();
        }
        boolean isNew = true;
        List<String> newConfigList = new ArrayList<>();
        for (String config : configs) {
            String configContent = getConfigContent(config, configName);
            if (configContent == null) {
                newConfigList.add(config);
            } else {
                newConfigList.add(configName + "=" + content);
                isNew = false;
            }
        }
        if (isNew) {
            newConfigList.add(configName + "=" + content);
        }
        StringJoiner stringJoiner = new StringJoiner("\n");
        newConfigList.forEach(stringJoiner::add);

        MyConfigProperties config = JvmConfig.getConfig();
        String configFilePath = FileUtil.getRealPath(config.getFilePath()) + CONFIG_FILE_NAME;
        FileUtil.saveFile(stringJoiner.toString(), configFilePath);

        configs.clear();
        configs.addAll(newConfigList);
    }

    public static void delete(String configName) throws Exception {
        if (configs.isEmpty()) {
            loadConfig();
        }
        List<String> newConfigList = new ArrayList<>();
        for (String config : configs) {
            String configContent = getConfigContent(config, configName);
            if (configContent == null) {
                newConfigList.add(config);
            }
        }
        StringJoiner stringJoiner = new StringJoiner("\n");
        newConfigList.forEach(stringJoiner::add);
        String configFilePath = FileUtil.getRealPath(JvmConfig.getConfig().getFilePath()) + CONFIG_FILE_NAME;
        FileUtil.saveFile(stringJoiner.toString(), configFilePath);

        configs.clear();
        configs.addAll(newConfigList);
    }

    private static String getConfigContent(String lineConfig, String configName) {
        String trimLineConfig = lineConfig.trim();
        if (trimLineConfig.startsWith(configName + "=")) {
            return trimLineConfig.substring(trimLineConfig.indexOf("=") + 1);
        }
        return null;
    }

    private static void loadConfig() throws Exception {
        if (!configs.isEmpty()) {
            configs.clear();
        }
        MyConfigProperties config = JvmConfig.getConfig();
        String configFilePath = FileUtil.getRealPath(config.getFilePath()) + CONFIG_FILE_NAME;
        File file = new File(configFilePath);
        List<String> textFileList = FileUtil.readTextFileList(file);
        configs.addAll(textFileList);
    }

}
